package com.xs.bookspoststation.UserActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.xs.bookspoststation.AdminActivity.AdminActivity;
import com.xs.bookspoststation.R;
import com.xs.bookspoststation.UserActivity2.UpdateUserPassWordActivity;
import com.xs.bookspoststation.UserActivity2.UpdateUserPwdActivity;
import com.xs.bookspoststation.UserActivity2.UserOperateInfoActivity;
import com.xs.bookspoststation.db.DBHelperUser;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //定义对象
    private EditText Edit_account,Edit_password;
    private RadioGroup login_radioGroup;
    private RadioButton login_radioButton1,login_radioButton2;
    private Button btn1_login,btn1_regist,btn1_up_pwd;
    //定义一个数据库帮助类
    private DBHelperUser dbHelperUser;
    //定义一个数据库操作类
    private SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定控件账号
        Edit_account = findViewById(R.id.Edit_account);
        //密码
        Edit_password = findViewById(R.id.Edit_password);
        //
        login_radioGroup = findViewById(R.id.login_radioGroup);
        //管理员
        login_radioButton1 = findViewById(R.id.login_radioButton1);
        //用户
        login_radioButton2 = findViewById(R.id.login_radioButton2);
        //登录
        btn1_login = findViewById(R.id.btn1_login);
        btn1_regist = findViewById(R.id.btn1_regist);
        btn1_up_pwd = findViewById(R.id.btn1_up_pwd);

        btn1_login.setOnClickListener(this);

    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1_login:
                //登录
                login();
                break;
            case R.id.btn1_regist:
                //注册
                Intent intent1 = new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(intent1);
                break;
            case R.id.btn1_up_pwd:
                //忘记密码
                Intent intent2 = new Intent(MainActivity.this, UpdateUserPassWordActivity.class);
                startActivity(intent2);
                break;
        }
    }

    /**
     * 登录
     */
    private void login() {
        //需要获取输入的用户名和密码
        String username = Edit_account.getText().toString();
        String password = Edit_password.getText().toString();
        //判断选择了哪种身份
        if (login_radioButton1.isChecked()==false && login_radioButton2.isChecked() == false){
            Toast.makeText(this, "请选择一种用户", Toast.LENGTH_SHORT).show();
        }else if (login_radioButton1.isChecked()){//选择管理员登录
            if (username.equals("admin") && password.equals("123456")){
                Intent intent=new Intent(MainActivity.this,AdminActivity.class);
                startActivity(intent);
                Toast.makeText(this, "欢迎进入管理员界面", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "您输入的用户名或者密码有误", Toast.LENGTH_SHORT).show();
            }
        }else {//选择用户登录
            if (login_radioButton2.isChecked()){
                //需要获取输入的用户名和密码
                final String username2=Edit_account.getText().toString();
                final String password2=Edit_password.getText().toString();
                //判断账号密码是否为空
                if (username2.equals("") || password2.equals("")){
                    Toast.makeText(MainActivity.this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    dbHelperUser = new DBHelperUser(MainActivity.this,"books_station.db",null,1);
                    db = dbHelperUser.getReadableDatabase();//数据库实例化
                    //游标接口进行对扫描数据库中的账号和密码
                    Cursor cursor = db.query("book_user",new String[]{"username","userpwd"},null,null,null,null,null);
                    boolean login = false;
                    //从数据库中匹配账号和密码
                    while (cursor.moveToNext()){//游标向下扫描
                        if (username2.equals(cursor.getString(cursor.getColumnIndex("username"))) &&
                                password2.equals(cursor.getString(cursor.getColumnIndex("userpwd")))){
                            login = true;
                            db.close();
                            break;
                        }
                    }
                    if (login){
                        //登录成功跳转
                        Intent intent = new Intent(MainActivity.this, UserOperateInfoActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putString("username", username2);//查找借阅书籍和改密码都用得上。
                        bundle.putString("userpwd", password2);//传密码过去，修改密码的时候使用
                        intent.putExtras(bundle);
                        startActivity(intent);
                        Toast.makeText(this, "欢迎进入！", Toast.LENGTH_SHORT).show();
                        finish();
                        return;
                    }else {
                        //账号密码在数据库中匹配不成功
                        Edit_account.setText("");
                        Edit_password.setText("");
                        Toast.makeText(this, "账号和密码不匹配或此账号不存在", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }


    }
}
